﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;



namespace Tesla.Util.UIUtilities
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TeslaLinkButton runat=server></{0}:TeslaLinkButton>")]
    
    public class TeslaLinkButton : WebControl
    {
        #region Local variables

        private Unit _Width = 100;                                            //default Width
        private Unit _Height = 20;                                            //default Height
        private Color _ForeColor = Color.Black;                               //default ForeColor
        private HorizontalAlign _TextHorizontalAlign = HorizontalAlign.Left;  //default TextHorizontalAlign
        private VerticalAlign _TextVerticalAlign = VerticalAlign.Middle;      //default TextVerticalAlign
        private string _ID = "";
        private string _NavigateUrl = "";
        private TargetType _Target = TargetType._self;                        //default Target
        private string _MouseOverImage;
        private string _MouseOutImage;
        private LinkButton _linkButton;

        #endregion

        #region Public types

        public enum TargetType { _blank, _self };

        #endregion

        #region Properties

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? string.Empty : s);
            }
            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Unit Width
        {
            get
            {
                if (ViewState["Width"] != null)
                {
                    Unit i = (Unit)ViewState["Width"];
                    return ((i == 0) ? _Width : i);
                }
                else
                    return _Width;
            }
            set
            {
                ViewState["Width"] = value;
                _Width = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Unit Height
        {
            get
            {
                return _Height;
            }
            set
            {
                _Height = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public HorizontalAlign TextHorizontalAlign
        {
            get { return _TextHorizontalAlign; }
            set { _TextHorizontalAlign = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public VerticalAlign TextVerticalAlign
        {
            get { return _TextVerticalAlign; }
            set { _TextVerticalAlign = value; }
        }

    
      

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public string NavigateUrl
        {
            get { return _NavigateUrl; }
            set { _NavigateUrl = value; }
        }

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public TargetType Target
        {
            get { return _Target; }
            set { _Target = value; }
        }

        [Category("Button Images")]
        [Browsable(true)]
        [Description("Set path to mouseover image file.")]
        [Editor("System.Web.UI.Design.UrlEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))]
        public string MouseOverImage
        {
            get { return _MouseOverImage; }
            set { _MouseOverImage = value; }
        }

        [Category("Button Images")]
        [Browsable(true)]
        [Description("Set path to mouseout image file.")]
        [Editor("System.Web.UI.Design.UrlEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))]
        public string MouseOutImage
        {
            get { return _MouseOutImage; }
            set { _MouseOutImage = value; }
        }

        #endregion

        #region Methods

        protected override void OnInit(EventArgs e)
        {
            string style = "background-image:url('" + MouseOutImage + "'); " +
                           "text-align:" + TextHorizontalAlign + "; ";
            if (TextVerticalAlign == VerticalAlign.Middle)
                style += ("line-height:" + Height.ToString() + "; ");
            string script = "";
            if (NavigateUrl != string.Empty)
                script = "window.open('" + NavigateUrl + "', '" + Target.ToString() + "')";

            _linkButton = new LinkButton();
            _linkButton.ID = ID;
            _linkButton.Attributes.Add("style", style);
            _linkButton.Attributes.Add("onmouseover", "document.getElementById('" + _linkButton.ID + "').style.backgroundImage = 'url(" + MouseOverImage + ")';");
            _linkButton.Attributes.Add("onmouseout", "document.getElementById('" + _linkButton.ID + "').style.backgroundImage = 'url(" + MouseOutImage + ")';");
            _linkButton.Attributes.Add("onclick", script);
            if (Target == TargetType._self)
                _linkButton.PostBackUrl = NavigateUrl;
            _linkButton.Width = Width;
            _linkButton.Height = Height;
            _linkButton.Font.Underline = false;
            _linkButton.ForeColor = ForeColor;
            _linkButton.Text = Text;
            base.OnInit(e);
        }
        protected override void CreateChildControls()
        {
            this.Controls.Add(_linkButton);
            ClearChildViewState();
            base.CreateChildControls();
        }

        #endregion

        
    }
}




